<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" pageOption />

    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">

    </ex_super_table_page>

    <ex_super_dialog_form ref="deviationHandlerDialog" v-bind="deviation_handler_dialog" @afterSubmitSuccess="reload()">
    </ex_super_dialog_form>
  </div>
</template>
<script>

import business_function from "@/common/business_function"

import superTablePageFunction from '@/mixin/super-table-page-function';
import page_table_model_manage from '@/mixin/page_table_model_manage';
import emitOnFunction from '@/mixin/emit-on-function';
let emitKey = 'oms>order_measure_deviation'
export default {
  title: 'label_order_measure_deviation',
  mixins: [superTablePageFunction, emitOnFunction(emitKey), page_table_model_manage,],
  components: {},
  data() {
    return {
      selection: [],
      selectStatusCount: {},
      option: {
        search: {
          default: { status: "", },
          model: [
            { key: "customerId", label: "label_customer", type: 'common_customer_choose' },
            { key: "receivingOrderCode", label: "label_inbound_order_code", },
            { key: "bizType", label: "label_business_type", type: 'select', enum: 'BizTypeEnum' },
            { key: "disposeStatus", label: "label_status", type: 'select', enum: "OrderDifferenceStatusEnum" },
            {
              key: 'startMeasureTime', label: 'label_measure_time', type: 'date-picker', placeholder: "label_start_time",
              attrs: { type: "datetime", 'default-time': '00:00:00', },
            },
            {
              key: 'endMeasureTime', label: '~', type: 'date-picker', placeholder: "label_end_time",
              attrs: { type: "datetime", 'default-time': '23:59:59', },
            },

            { key: "disposeUserId", label: "label_handler_user", type: 'common_user_select', },
            {
              key: 'startDisposeTime', label: 'label_start_handler_time', type: 'date-picker',
              attrs: { type: "datetime", 'default-time': '00:00:00' },
            },
            {
              key: 'endDisposeTime', label: 'label_end_handler_time', type: 'date-picker',
              attrs: { type: "datetime", 'default-time': '23:59:59' },
            },
          ],
        },
        actionExtra: [
          {
            label: 'label_export', auth: 'oms_order_measure_deviation_export', click: this.exportExcel
          },
        ],
        table: {
          url: "/oms-api/anomaly/OmsBaseOrderDifferenceRpc/selectPage.json",
          model: this.init_table_model(),
        }
      },

      deviation_handler_dialog: {
        customClass: 'dialog_auto',
        submitUrl: "/oms-api/anomaly/OmsBaseOrderDifferenceRpc/dispose.json",
        submitDataHandle: (data) => {

          return {
            id: data.id,
            disposeMode: data.disposeMode,
            disposeDifferenceRate: data.disposeMode == 'ACCEPT_DIFFERENCES' ? 100 : data.disposeMode == 'NOT_ACCEPT_DIFFERENCES' ? 0 : data.disposeDifferenceRate,
            attachedList: data.attachedList,
          }
        },
        form: {
          itemClass: "form_item_default", inputClass: "form_input_default",
          formClass: "form_default_col2 flex_row_wrap",
          default: {attachedList:[],},
          model: [
            { key: 'receivingOrderCode', label: 'label_inbound_code', formType: 'text', },

            {
              key: 'customerName', label: 'label_customer_name', formType: 'text',
              formatter: (r, c, v) => this.$formatNameCode(v, r.customerCode)
            },
            {
              key: 'totalWeight', label: 'label_order_weight', formType: 'text',
              formatter: (r, c, v, i) => this.$addUnit(v, "kg")

            },
            {
              key: 'totalVolume', label: 'label_measure_volume', formType: 'text',
              formatter: (r, c, v, i) => this.$addUnit(v, "m³")
            },
            {
              key: 'bizType', label: 'label_business_type', formType: 'text',
              formatter: (r, c, v, i) => this.$enumShow(v, "BizTypeEnum")
            },
            {
              key: 'deviationRate', label: 'label_customer_config_deviation_rate', formType: 'text',
              formatter: (r, c, v, i) => this.$addUnit(v ? v : "0", "%")
            },



            { divider: true },
            {
              key: 'isTransitMeasure', label: 'label_is_transfer_measure', formType: 'text',
              formatter: (r, c, v, i) => this.$enumShow(v ? v : "N", "YesOrNoEnum")
            },
            {
              key: 'transitMeasureTime', label: 'label_transfer_measure_time', formType: 'text',
              if: (data) => data.isTransitMeasure == "Y",
              render: (h, { data, item }) => this.$renderText(h, this.$dateFormat(data[item.key]))

            },
            {
              key: 'transitMeasureTotalWeight', if: (data) => data.isTransitMeasure == "Y",
              label: 'label_transit_measure_weight', formType: 'text',
              formatter: (r, c, v, i) => this.$addUnit(v, "kg")
            },
            {
              key: 'transitMeasureTotalVolume', if: (data) => data.isTransitMeasure == "Y",
              label: 'label_transit_measure_volume', formType: 'text',
              formatter: (r, c, v, i) => this.$addUnit(v, "m³")
            },
            {
              key: 'transitMeasureDeviationRate', label: 'label_transfer_measure_deviation_rate', formType: 'text',
              if: (data) => data.isTransitMeasure == "Y",
              render: (h, { data, item }) => this.$renderText(h, this.$addUnit(data[item.key], "%"))

            },
            {
              key: 'transitMeasureUserName', if: (data) => data.isTransitMeasure == "Y",
              label: 'label_transit_measure_user', formType: 'text',
              formatter: (r, c, v, i) => this.$formatNameCode(v, r.transitMeasureUserCode)
            },
            { divider: true, },
            {
              key: 'disposeMode', label: 'label_do_with_method', type: 'select',
              enum: 'OrderDifferenceDisposeModeEnum',
            },
            {
              key: 'disposeDifferenceRate', label: 'label_dispose_difference_rate', type: 'select', if: (data) => data.disposeMode == "ACCEPT_RATE",
              attrs: {
                items: [
                  { _label: '50%', _value: 50 },
                  { _label: '60%', _value: 60 },
                  { _label: '70%', _value: 70 },
                  { _label: '80%', _value: 80 },
                  { _label: '90%', _value: 90 },
                ],
              }
            },
            {
              key: 'attachedList', label: 'label_handler_attachment', type: 'ex_super_upload',
              attrs: {
                type: "file",
                limit: 10,
              }
            },
          ],
          rules: {
            'disposeMode': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            'disposeDifferenceRate': { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
            // 'attachedList': { type: "array", required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
          },
        }
      },
    };
  },
  methods: {
    option_table_model() {
      return [
        { type: 'selection', clickToggle: true, keyGroup: 'label_table_operate' },

        { key: 'receivingOrderCode', label: 'label_inbound_code', 'min-width': 200, 'show-overflow-tooltip': true, },
        {
          key: 'bizType', label: 'label_business_type', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v, "BizTypeEnum")
        },
        { key: 'customerName', label: 'label_customer_name', 'width': 100, 'show-overflow-tooltip': true, },
        { key: 'customerCode', label: 'label_customer_code', 'width': 100, 'show-overflow-tooltip': true, },
        {
          key: 'totalWeight', label: 'label_order_weight', 'width': 120,
          formatter: (r, c, v, i) => this.$addUnit(v, "kg")

        },
        {
          key: 'totalVolume', label: 'label_measure_volume', 'width': 120,
          formatter: (r, c, v, i) => this.$addUnit(v, "m³")

        },

        {
          key: 'status', label: 'label_status', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v, "OrderDifferenceStatusEnum")
        },


        {
          key: 'deviationRate', label: 'label_customer_config_deviation_rate', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$addUnit(v ? v : "0", "%")
        },


        {
          key: 'isTransitMeasure', label: 'label_is_transfer_measure', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$enumShow(v ? v : "N", "YesOrNoEnum")
        },
        {
          key: 'transitMeasureTime', label: 'label_transfer_measure_time', 'width': 120, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$dateFormat(row[column.property]))
        },
        {
          key: 'transitMeasureTotalWeight',
          label: 'label_transit_measure_weight', 'width': 120,
          formatter: (r, c, v, i) => this.$addUnit(v, "kg")
        },
        {
          key: 'transitMeasureTotalVolume',
          label: 'label_transit_measure_volume', 'width': 120,
          formatter: (r, c, v, i) => this.$addUnit(v, "m³")
        },
        {
          key: 'transitMeasureDeviationRate', label: 'label_transfer_measure_deviation_rate', 'width': 120, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$addUnit(row[column.property], "%"))
        },
        {
          key: 'transitMeasureUserName',
          label: 'label_transit_measure_user', 'width': 120, 'show-overflow-tooltip': true,
          formatter: (r, c, v, i) => this.$formatNameCode(v, r.transitMeasureUserCode)
        },


        {
          key: 'disposeMode', label: 'label_do_with_method', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$enumShow(row[column.property], "OrderDifferenceDisposeModeEnum"))

        },
        {
          key: 'disposeDifferenceRate', label: 'label_process_variance_ratio', 'width': 120, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$addUnit(row[column.property], "%"))
        },
        {
          key: 'disposeUserName', label: 'label_handler_user', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, (row[column.property]))

        },
        {
          key: 'disposeTime', label: 'label_handler_time', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$dateFormat(row[column.property]))
        },

        {
          key: 'confirmUserName', label: 'label_confirm_user', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, (row[column.property]))

        },
        {
          key: 'confirmTime', label: 'label_confirm_time', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$dateFormat(row[column.property]))
        },

        {
          key: 'closeUserName', label: 'label_close_user', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, (row[column.property]))

        },
        {
          key: 'closeTime', label: 'label_close_time', 'width': 140, 'show-overflow-tooltip': true,
          render: (h, { row, column }) => this.$renderText(h, this.$dateFormat(row[column.property]))
        },
        {
          type: 'operate', width: 170, fixed: 'right', operate: [

            {
              label: 'label_view_detail', click: (row) => {
                this.$router.push({
                  path: '/page/oms/anomaly/order_measure_detail', query: { id: row.id, }
                })
              }
            },
            {
              type: 'more',
              items: [
                {
                  label: 'label_deviation_handler',
                  auth: 'oms_order_measure_deviation_handler',

                  disabled: (row) => !['WAIT_DISPOSE'].includes(row.status),

                  click: (row) => {
                    this['$refs']['deviationHandlerDialog'].showNew({
                      title: this.$tt("label_deviation_handler"),
                      data: Object.assign({ attachedList: [] }, row),
                      onlySetData: true,
                    })
                  }
                },
                {
                  label: 'label_deviation_confirm',
                  auth: 'oms_order_measure_deviation_confirm',

                  disabled: (row) => !['WAIT_CONFIRM'].includes(row.status),
                  click: (row) => {
                    this.operateConfirm(row)
                  }
                },
                {
                  label: 'label_deviation_close',
                  auth: 'oms_order_measure_deviation_close',

                  disabled: (row) => !['WAIT_CONFIRM', "WAIT_DISPOSE"].includes(row.status),
                  click: (row) => {
                    this.operateClose(row)
                  }
                },
              ]
            }
          ]
        }
      ]
    },


    operateConfirm(row) {
      this.$confirm(this.$tt('选择的订单 {order} 测量差异的处理方案是否确认？确认后差异就此完结，无需再进行跟进处理。', { order: row.receivingOrderCode }), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/oms-api/anomaly/OmsBaseOrderDifferenceRpc/confirm.json",
          method: 'post', type: "json",
          loading: true,
          data: {
            id: row.id
          },
          success: (res) => {
            this.$message({
              showClose: true,
              message: this.$tt('tip_submit_success'),
              type: 'success'
            })
            this.reload()
          }
        })
      }).catch(() => {
      });

    },
    operateClose(row) {
      this.$confirm(this.$tt('选择的订单 {order} 测量差异的处理方案是否关闭？关闭后无需再进行跟进处理，订单体积和重量不做任何影响和变更。', { order: row.receivingOrderCode }), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.$ajax({
          url: "/oms-api/anomaly/OmsBaseOrderDifferenceRpc/close.json",
          method: 'post', type: "json",
          loading: true,
          data: {
            id: row.id
          },
          success: (res) => {
            this.$message({
              showClose: true,
              message: this.$tt('tip_submit_success'),
              type: 'success'
            })
            this.reload()
          }
        })
      }).catch(() => {
      });

    },


    exportExcel() {

      business_function.confirmOperateBaseSelectOrForm.call(this, {
        data: this.$refs.SuperTablePage.getSearchFormData(),
        model: this.option.search.model,
        callback: (selection, data) => {
          let params = data
          this.$functionCommon({
            url: "/oms-api/anomaly/OmsBaseOrderDifferenceRpc/export.json",
            data: params, type: 'form',
          })
        }
      })

    },
  }
};

</script>
<style></style>
